home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Graphics Plus
/
Graphics Plus.iso
/
general
/
viewers
/
polyview
/
polyvw31.lha
/
Polyview3.1
/
new
/
pvparse.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-23
|
16KB
|
519 lines
/*****************************************************************************
* NCSA Polyview 3.0 *
* *
* Version 3 changes and additions by Marc Andreessen. *
* Version 2 by Brian Calvert. *
* *
* Software Development Group *
* National Center for Supercomputing Applications *
* University of Illinois at Urbana-Champaign *
* *
* This is BETA release software. As such it may contain software bugs and *
* exhibit inconsistencies. *
* *
* Please send bug reports to polyview@ncsa.uiuc.edu. *
* *
* Copyright (c) 1992 The Board of Trustees of the University of Illinois. *
* *
* Permission to use, copy, and modify this software and its *
* documentation for educational, research, and non-profit purposes is *
* hereby granted, provided that the above copyright notice, the original *
* authors names, and this permission notice appear in all such copies. *
* Any distribution of this software requires the explicit and written *
* authorization of the authors. *
* *
* The University of Illinois makes no representations about the *
* suitability of this software for any purpose. It is provided "as is" *
* without warranty of any kind. *
*****************************************************************************/
/* $Header: /usr3/people/gbourhis/pv3/new/RCS/pvparse.c,v 1.1 92/09/18 10:55:26 marca Exp $ */
#ifdef RCSLOG
$Log: pvparse.c,v $
* Revision 1.1 92/09/18 10:55:26 marca
* Initial revision
*
#endif
#include "pv.h"
/*
ACTION FORMAT: The syntax of user-entered commands is defined by the action
format. An action format is a string of mandatory and optional keywords and
values (collectively referred to as terms).
Mandatory terms are simply stated in the format string. Optional terms are
enclosed in square brackets. Optional terms may be nested.
Terms may be either keywords or values. Keywords are simply stated as
alphanumeric strings. The syntax of values are enclosed in angle brackets.
*/
/* Additions:
help option
identify where the syntax error occurs
identify all the commands that an ambiguous entry matches
prompt for additional information (for interactive only?)
*/
#define COMMENT_CHAR ';'
char *get_keyword_arg(int argc, arg_t arg[], int keynum)
{
/* If the keyword number is larger than the number of arguments, */
/* then it doesn't exist. */
if (keynum >= argc)
return NULL;
/* If the argument is a keyword, return its value. Otherwise, */
/* return NULL to indicate no match. */
if (arg[keynum].type == PVKEYWORD)
return arg[keynum].value;
else
return NULL;
}
char * get_string_arg(int argc, arg_t arg[], char *name, char *def)
{
for (argc--; argc > 0; argc--)
if ((arg[argc].type == PVSTRING) &&
(strcasecmp(arg[argc].description, name) == 0))
return arg[argc].value;
/* If no match was made, return the default. */
return def;
}
float get_float_arg(int argc, arg_t arg[], char *name, float def)
{
for (argc--; argc > 0; argc--)
if ((arg[argc].type == PVFLOAT) &&
(strcasecmp(arg[argc].description, name) == 0))
return ((float)atof (arg[argc].value));
/* If no match was made, return the default. */
return def;
}
static int match_term(char [], char [], int *, arg_t []);
static char * get_token(char **, char *, char, char);
static char * get_linetoken(char **, char *);
static char * get_formattoken(char **, char *);
int load_script (state_t *state, FILE *fp)
{
char line[MAXLINELEN];
while (fgets(line, MAXLINELEN, fp) != NULL)
{
line[strlen(line)-1] = '\0';
parse_line(state, line, FALSE);
}
return ST_OKAY;
}
int parse_line (state_t *state, char *line, int immediate)
{
char formattoken[MAXFORMATLEN];
char linetoken[MAXFORMATLEN];
char *lineptr;
char *tokenptr;
parse_t *parseptr;
parse_t *matchptr;
int matches;
int match;
int i;
int argc;
int margc;
arg_t arg[MAXACTIONARGS];
arg_t marg[MAXACTIONARGS];
action_t *action;
stprintf (state, " ");
/* Echo the line to the transcript file. */
/* BUT DON'T WRITE QUIT! */
/* Also don't write 'do filename'. */
if (state->transcript_fp != NULL &&
strncmp (line, "quit", 4) != 0 &&
strncmp (line, "do ", 3) != 0 &&
strncmp (line, "script", 6) != 0)
fprintf(state->transcript_fp, "%s\n", line);
/* Strip off anything that follows a comment character. */
if ( (lineptr = strchr(line, (int) COMMENT_CHAR)) != NULL)
*lineptr = '\0';
/* Skip any leading blanks. If there is nothing to the remaining */
/* string, exit. */
for (lineptr = line; *lineptr == ' '; lineptr++);
if (*lineptr == '\0')
return ST_OKAY;
/* Add to command box's history list; since we wait until now,
we make sure comments/blank lines aren't put into list. */
if (state->last_cmd_already_added)
state->last_cmd_already_added = FALSE;
else
{
if (strncmp (line, "do", 2) != 0 &&
strncmp (line, "script", 6) != 0)
GUIaddCommand (line);
}
matches = 0;
for (parseptr = state->parse_table; parseptr->format[0] != '\0';
parseptr++)
{
match = TRUE;
lineptr = line;
tokenptr = parseptr->format;
argc = 0;
while ( ((*tokenptr) || (*lineptr)) && match)
{
get_linetoken(&lineptr, linetoken);
get_formattoken(&tokenptr, formattoken);
fflush (stdout);
if (match_term(linetoken, formattoken, &argc, arg) != ST_OKAY)
match = FALSE;
fflush (stdout);
}
if (match)
{
matchptr = parseptr;
matches++;
fflush (stdout);
margc = argc;
for (i = 0; i < argc; i++)
marg[i] = arg[i];
}
}
if (matches < 1)
{
stprintf(state, "SYNTAX ERROR: %s\n", line);
return ST_ERROR;
}
else if (matches > 1)
{
stprintf(state, "AMBIGUOUS: %s\n", line);
return ST_ERROR;
}
/* Either immediately execute the command or add it to the action */
/* list. */
if (immediate || matchptr->immediate)
{
if (matchptr->action_fn == NULL)
printf("Immediate execution, but NULL pointer!\n");
else
{
/* Create a new action record and pass it to the */
/* function. */
action = new_action(state, matchptr->action_fn,
margc, marg, /*steps*/ 1);
action->global = matchptr->global;
do_action(state, action);
free_action(state, action);
}
}
else
add_action(state, matchptr->action_fn, margc, marg,
/*steps*/ 1, matchptr->global, /*at_current*/ FALSE);
return ST_OKAY;
}
int AddArg (int *argc, arg_t *arg, char *term_format, char *term)
{
/* Dummy string for test for float. */
char float_test[5];
/* Copy the name of the value into the arg descriptor table. */
arg[*argc].description = (char *) PVMALLOC(strlen(term_format)-4);
sscanf(&term_format[4], "%[^>]>", arg[*argc].description);
/* If the term_format is a value, then get the type of the value */
/* and attempt to read the value into a malloc'd data area. Set */
/* the arg type and value. */
switch (term_format[2]) {
case 's':
arg[*argc].type = PVSTRING;
arg[*argc].value = (char *) PVMALLOC(strlen(term)+1);
strcpy(arg[*argc].value, term);
break;
case 'f':
arg[*argc].type = PVFLOAT;
arg[*argc].value = (char *) PVMALLOC(strlen (term)+1);
strcpy (arg[*argc].value, term);
arg[*argc].value[strlen (term)] = '\0';
/* Check to see if we're dealing with a float. */
if (sscanf(term, "%f", (float *)float_test) != 1)
{
arg[*argc].type = PVDEFAULT;
PVFREE(arg[*argc].description);
PVFREE(arg[*argc].value);
return ST_ERROR;
}
break;
default:
printf("SYSTEM ERROR: '%s' is not ", term_format);
printf("supported by match_term.\n");
return ST_ERROR;
}
return ST_OKAY;
}
static int match_term (char term[], char term_format[],
int *argc, arg_t arg[])
{
char *term_formatptr;
char subterm[MAXFORMATLEN];
int match_count;
fflush (stdout);
if (strncmp(term_format, "<%", 2) == 0)
{
if (*term == '\0')
{
/* Return ST_EMPTYSTRING if term is empty since there's */
/* nothing to parse. */
return ST_EMPTYSTRING;
}
/* Add the argument to the list. */
if (AddArg(argc, arg, term_format, term) == ST_ERROR)
{
return ST_ERROR;
}
}
else if (term_format[0] == '[')
{
/* If the term format is an optional parameter, strip the brackets */
/* from the end of the string and pass a pointer to the character */
/* after the opening bracket back into the routine so that it can */
/* be recursively parsed. */
term_format[strlen(term_format)-1] = '\0';
if (match_term(term, &term_format[1], argc, arg) == ST_ERROR)
return ST_ERROR;
else
return ST_OKAY;
}
else if (strpbrk(term_format, "|") != NULL)
{
term_formatptr = term_format;
if (*term == '\0')
{
/* If the term does not exist, then default to the */
/* first subterm of the ORed list. */
get_token(&term_formatptr, subterm, '|', '\0');
/* If the term is a keyword, return it as an */
/* argument just in case this is an optional */
/* list (in optional ORed lists of keyowrds, the */
/* first keyword is the default value. */
if ( (subterm[0] != '<') && (subterm[0] != '[') )
{
arg[*argc].description = (char *) PVMALLOC(sizeof(char));
*(arg[*argc].description) = '\0';
arg[*argc].type = PVKEYWORD;
arg[*argc].value = (char *) PVMALLOC(strlen(subterm)+1);
strcpy(arg[*argc].value, subterm);
(*argc)++;
}
return ST_EMPTYSTRING;
}
else
{
/* If the term format is an ORed set of options, */
/* then beak it down into subterms and recursively */
/* attempt to match them to the term. */
match_count = 0;
while (*term_formatptr != '\0')
{
/* Get the next ORed subterm from the term */
/* and call this function again to parse it. */
get_token(&term_formatptr, subterm, '|', '\0');
if (match_term(term, subterm, argc, arg) == ST_OKAY)
match_count++;
}
if (match_count == 1)
/* If only one subterm matched, then the ORed list */
/* was successfully matched. */
return ST_OKAY;
}
return ST_ERROR;
}
else if (*term == '\0')
{
if (*term_format == '\0')
return ST_OKAY;
else
/* Return ST_EMPTYSTRING if the term is empty but the format */
/* is not. */
return ST_EMPTYSTRING;
}
else
{
/* If term_format is not a value, set the arg type to PVKEYWORD */
/* and arg value to the keyword string. */
/* There is a mismatch error if the term is empty but the */
/* format is required, or if the given characters of the */
/* term don't match the corresponding characters of the */
/* term_format. */
if ( ((strlen(term) == 0) && (strlen(term_format) != 0)) ||
(strncasecmp(term, term_format, strlen(term)) != 0) )
return ST_ERROR;
arg[*argc].description = (char *) PVMALLOC(sizeof(char));
*(arg[*argc].description) = '\0';
arg[*argc].type = PVKEYWORD;
arg[*argc].value = (char *) PVMALLOC(strlen(term_format)+1);
strcpy(arg[*argc].value, term_format);
}
/* Increment the argument count. */
(*argc)++;
return ST_OKAY;
}
static char *skip_blanks(char *str)
{
if (str == NULL)
return NULL;
while ( ((*str == ' ') || (*str == '\t')) && (*str != '\0') )
str++;
return str;
}
static char *get_token(char **token, char *copy, char endtoken, char captoken)
{
char *returnptr;
/* We will return the pointer to the beginning of the copy buffer. */
returnptr = copy;
/* Copy the token until we reach the terminating character, the */
/* end of the string, or a comment character. */
for (; (**token != endtoken) && (**token != '\0') &&
(**token != COMMENT_CHAR); copy++, (*token)++)
*copy = **token;
/* If we haven't hit the end of the token string yet, increment the */
/* token string pointer. */
if ((**token != '\0') && (**token != COMMENT_CHAR))
(*token)++;
/* Terminate the string with the closing character provided by the */
/* caller, and a zero character (which may be redundant). */
*(copy++) = captoken;
*(copy) = '\0';
return returnptr;
}
static char *get_linetoken(char **linestring, char *copystring)
{
char *copychar;
/* Initialize the return string by making its first character a */
/* string-terminator. */
*copystring = '\0';
copychar = copystring;
/* Skip any leading blanks in the token string. */
*linestring = skip_blanks(*linestring);
/* Check the first character of the string to determine whether */
/* it is a comment (';'), a quoted string ('\"'), or a regular */
/* keyword (anything else). */
switch (**linestring)
{
case COMMENT_CHAR: /* Commment */
/* If we've hit a comment, return a pointer to an empty */
/* string. This signals the calling routine that there's */
/* nothing more to read. */
*linestring = copystring;
break;
case '\"': /* Quoted string token */
(*linestring)++;
get_token(linestring, copychar, '\"', '\0');
break;
default: /* Keyword token */
get_token(linestring, copychar, ' ', '\0');
break;
}
return copychar;
}
static char *get_formattoken(char **formatstring, char *copystring)
{
char *copychar;
/* Initialize the return string by making its first character a */
/* string-terminator. */
*copystring = '\0';
copychar = copystring;
/* Skip any leading blanks in the token string. */
*formatstring = skip_blanks(*formatstring);
/* Check the first character of the string to determine whether */
/* it is an optional parameter ('['), a value ('<'), the end of */
/* the string ('\0'), or a keyword (anything else). */
switch (**formatstring)
{
case '[': /* Optional token */
get_token(formatstring, copychar, ']', ']');
break;
case '<': /* Value token */
get_token(formatstring, copychar, '>', '>');
break;
case '\0': /* No token */
break;
default: /* Keyword token */
get_token(formatstring, copychar, ' ', '\0');
break;
}
return copystring;
}